import {
	wxConfig
} from '../api/common/index.js';
var jweixin = require('jweixin-module');

export default {
	//判断是否在微信中  
	isWechat: function() {
		var ua = window.navigator.userAgent.toLowerCase();
		if (ua.match(/micromessenger/i) == 'micromessenger') {
			return true;
		} else {
			return false;
		}
	},
	//初始化sdk配置  
	initJssdk: function(callback, url) {
		wxConfig({
			url
		}).then(res => {
			if (res.data) {
				jweixin.config({
					debug: false,
					appId: res.data.appid,
					timestamp: res.data.timestamp,
					nonceStr: res.data.noncestr,
					signature: res.data.signature,
					jsApiList: [
						// 'checkJsApi',
						'onMenuShareTimeline',
						'onMenuShareAppMessage'
					]
				});
				//配置完成后，再执行分享等功能  
				if (callback) {
					callback(res.data);
				}
			}
		});
	},
	//在需要自定义分享的页面中调用  
	share: function(data, url) {
		url = url ? url : window.location.href;
		if (!this.isWechat()) {
			return;
		}
		//每次都需要重新初始化配置，才可以进行分享  
		this.initJssdk(function(signData) {
			jweixin.ready(function() {
				var shareData = {
					title: data && data.title ? data.title : "冻品交易港",
					desc: data && data.desc ? data.desc : "",
					link: url,
					imgUrl: data && data.img ? data.img :
						"https://oss-shop.cciinet.com/share/20201113105230.png",
					success: function(res) {
						// 用户点击分享后的回调
					},
					cancel: function(res) {}
				};
				//分享给朋友接口  
				jweixin.onMenuShareAppMessage(shareData);
				//分享到朋友圈接口  
				jweixin.onMenuShareTimeline(shareData);
			});
		}, url);
	}
}
